$(function() {
	var timeZoneID = $('#hiddenTimeZoneID').val();
	var $updateTable = $('#updateTable');
	var $setupTable = $('#setupTable');
	
	initHistoryTable();
	initListener();
	
	function initListener() {
		$(window).resize(function () {
			$updateTable.bootstrapTable('resetView', {
	            height: getTableHeight()
	        });
			$setupTable.bootstrapTable('resetView', {
	            height: getTableHeight()
	        });
	    });
	}
	
	function getTableHeight() {
	    return $(window).height() - 150;
	}
	
	function exportUpdateResult(data) {
		$('#downloadFrame').prop('src', 'jobs/update/export/' + data.serial);
	}
	
	function exportSetupResult(data) {
		$('#downloadFrame').prop('src', 'jobs/setup/export/' + data.serial);
	}
	
	function initHistoryTable() {
		
		$updateTable.bootstrapTable({
			height: getTableHeight(),
			url: 'jobs/update/history',
			idField: 'id',
			pagination: true,
			sidePagination: 'server',
			showRefresh: true,
			showToggle: true,
			showColumns: true,
			toolbar: '#updateTableToolbar',
			iconSize: 'outline',
			icons: {
				paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
	            paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
	            refresh: 'glyphicon-repeat',
				toggle: 'glyphicon-list-alt',
				columns: 'glyphicon-list',
	            detailOpen: 'glyphicon-plus icon-plus',
	            detailClose: 'glyphicon-minus icon-minus'
			},
			columns: [
				{
					field: 'id',
					title: 'ID',
					align: 'center',
					visible: false
				},
				{
					field: 'serial',
					title: 'serial',
					align: 'center',
					visible: false
				},
				{
					field: 'minerSource',
					title: '升级方式',
					halign: 'center',
					align: 'left',
					width: 100,
					formatter: function(value, row, index) {
						if (value == '1') {
							return '按机架';
						}
						if (value == '2') {
							return '按IP列表';
						}
						return value;
					}
				},
				{
					field: 'firmwareName',
					title: '固件名称',
					halign: 'center',
					align: 'left'
				},
				{
					field: 'firmwareVersion',
					title: '固件版本',
					halign: 'center',
					align: 'left'
				},
				{
					field: 'totalCount',
					title: '矿机数',
					halign: 'center',
					align: 'right'
				},
				{
					field: 'detailCount',
					title: '成功数',
					halign: 'center',
					align: 'right'
				},
				{
					field: 'failureCount',
					title: '失败数',
					halign: 'center',
					align: 'right'
				},
				{
					field: 'executeTime',
					title: '升级时间',
					align: 'center',
					formatter: function(value, row, index) {
						return moment.tz(value, timeZoneID).format('YYYY-MM-DD HH:mm:ss') + '(' + timeZoneID + ')';
					}
				},
				{
					field: 'status',
					title: '任务状态',
					align: 'center',
					formatter: function(value, row, index) {
						if (value == 2) {
							return '执行完成';
						}
						if (value == 1) {
							return '正在执行';
						}
						if (value == 0) {
							return '等待执行';
						}
						if (value < 0) {
							return '任务无效';
						}
						return value;
					}
				},
				{
					field: 'operate',
					title: '操作',
					align: 'center',
					events: window.operateEvents = {
							'click .export': function (e, value, row, index) {
								exportUpdateResult(row);
					        }
					},
					formatter: function(value, row, index) {
						if (row.status != 2) {
							return '';
						}
						return [
								'<a class="export" href="javascript:void(0)" title="导出升级结果">',
								'<i class="glyphicon glyphicon-export"></i>',
								'</a>'
						       ].join('');
					}
				}
			]
		});
		
		$setupTable.bootstrapTable({
			height: getTableHeight(),
			url: 'jobs/setup/history',
			idField: 'id',
			pagination: true,
			sidePagination: 'server',
			showRefresh: true,
			showToggle: true,
			showColumns: true,
			toolbar: '#setupTableToolbar',
			iconSize: 'outline',
			icons: {
				paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
	            paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
	            refresh: 'glyphicon-repeat',
				toggle: 'glyphicon-list-alt',
				columns: 'glyphicon-list',
	            detailOpen: 'glyphicon-plus icon-plus',
	            detailClose: 'glyphicon-minus icon-minus'
			},
			columns: [
				[
					{
						field: 'id',
						title: 'ID',
						align: 'center',
						valign: 'middle',
						visible: false,
						rowspan: 2
					},
					{
						field: 'serial',
						title: 'serial',
						align: 'center',
						valign: 'middle',
						visible: false,
						rowspan: 2
					},
					{
						field: 'minerSource',
						title: '配置方式',
						halign: 'center',
						align: 'left',
						valign: 'middle',
						width: 100,
						rowspan: 2,
						formatter: function(value, row, index) {
							if (value == '1') {
								return '按机架';
							}
							if (value == '2') {
								return '按IP列表';
							}
							return value;
						}
					},
					{
						field: 'pools',
						title: '矿池信息',
						align: 'center',
						colspan: 3
					},
					{
						field: 'totalCount',
						title: '矿机数',
						halign: 'center',
						align: 'right',
						valign: 'middle',
						rowspan: 2
					},
					{
						field: 'detailCount',
						title: '成功数',
						align: 'center',
						align: 'right',
						valign: 'middle',
						rowspan: 2
					},
					{
						field: 'failureCount',
						title: '失败数',
						align: 'center',
						align: 'right',
						valign: 'middle',
						rowspan: 2
					},
					{
						field: 'executeTime',
						title: '配置时间',
						align: 'center',
						valign: 'middle',
						rowspan: 2,
						formatter: function(value, row, index) {
							return moment.tz(value, timeZoneID).format('YYYY-MM-DD HH:mm:ss') + '(' + timeZoneID + ')';
						}
					},
					{
						field: 'status',
						title: '任务状态',
						align: 'center',
						valign: 'middle',
						rowspan: 2,
						formatter: function(value, row, index) {
							if (value == 2) {
								return '执行完成';
							}
							if (value == 1) {
								return '正在执行';
							}
							if (value == 0) {
								return '等待执行';
							}
							if (value < 0) {
								return '任务无效';
							}
							return value;
						}
					},
					{
						field: 'operate',
						title: '操作',
						align: 'center',
						valign: 'middle',
						rowspan: 2,
						events: window.operateEvents = {
								'click .export': function (e, value, row, index) {
									exportSetupResult(row);
						        }
						},
						formatter: function(value, row, index) {
							if (row.status != 2) {
								return '';
							}
							return [
									'<a class="export" href="javascript:void(0)" title="导出配置结果">',
									'<i class="glyphicon glyphicon-export"></i>',
									'</a>'
							       ].join('');
						}
					}
				],
				[
					{
						field: 'pool1',
						title: '1',
						halign: 'center',
						align: 'left',
						formatter: function(value, row, index) {
							var pools = row.pools;
							if (pools && pools.length >= 1) {
								var pool = pools[0];
								var url = pool.url ? pool.url : '-';
								var worker = pool.username ? pool.username : '-';
								return url + '<br>' + worker;
							}
						}
					},
					{
						field: 'pool2',
						title: '2',
						halign: 'center',
						align: 'left',
						formatter: function(value, row, index) {
							var pools = row.pools;
							if (pools && pools.length >= 2) {
								var pool = pools[1];
								var url = pool.url ? pool.url : '-';
								var worker = pool.username ? pool.username : '-';
								return url + '<br>' + worker;
							}
						}
					},
					{
						field: 'pool3',
						title: '3',
						halign: 'center',
						align: 'left',
						formatter: function(value, row, index) {
							var pools = row.pools;
							if (pools && pools.length >= 3) {
								var pool = pools[2];
								var url = pool.url ? pool.url : '-';
								var worker = pool.username ? pool.username : '-';
								return url + '<br>' + worker;
							}
						}
					}
				]
			]
		});
	}
});